import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

/**
 * 可展开的文本text
 * 上部文本
 * 下部展开按钮
 */
// ignore: must_be_immutable
class OPenButtonRichText extends StatefulWidget {
  OPenButtonRichText(
      {Key? key,
      this.text,
      this.width,
      this.textColor = Colors.black,
      this.textSize = 16})
      : super(key: key);
  double? width;
  String? text;
  Color? textColor;
  double? textSize;

  @override
  _OPRichText createState() => _OPRichText();
}

class _OPRichText extends State<OPenButtonRichText> {
  // 全文、收起 的状态
  bool mIsExpansion = false;
  int mSelectIndex = -1; //默认未选中
  bool IsExpansion(String text, width) {
    TextPainter _textPainter = TextPainter(
        maxLines: 3,
        text: TextSpan(
            text: text, style: TextStyle(fontSize: 14.0, color: Colors.black)),
        textDirection: TextDirection.ltr)
      ..layout(maxWidth: width - 180, minWidth: 50);
    if (_textPainter.didExceedMaxLines) {
      //判断 文本是否需要截断
      return true;
    } else {
      return false;
    }
  }

  void _isShowText() {
    if (mIsExpansion) {
      //关闭
      setState(() {
        mIsExpansion = false;
      });
    } else {
      //打开
      setState(() {
        mIsExpansion = true;
      });
    }
  }

  // ignore: non_constant_identifier_names
  Widget _RichText(String _text, width) {
    if (IsExpansion(_text, width)) {
      //是否截断
      if (mIsExpansion) {
        return GestureDetector(
          behavior: HitTestBehavior.opaque,
          onTap: () {
            _isShowText();
          },
          child: Text(
            _text,
            style:
                TextStyle(color: widget.textColor, fontSize: widget.textSize!),
          ),
        );
      } else {
        return Column(
          children: <Widget>[
            GestureDetector(
              behavior: HitTestBehavior.opaque,
              onTap: () {
                _isShowText();
              },
              child: Text(
                _text,
                style: TextStyle(
                    color: widget.textColor, fontSize: widget.textSize!),
                maxLines: 3,
              ),
            ),
            SizedBox(
              height: 15,
            ),
            Container(
              decoration: ShapeDecoration(
                  color: Color(0xffDDDDDD),
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.all(Radius.circular(3)))),
              child: GestureDetector(
                behavior: HitTestBehavior.opaque,
                onTap: () {
                  _isShowText();
                },
                child: Container(
                  // width: widget.width,
                  height: 40,
                  child: Center(
                    child: Text(
                      '点击查看全文',
                      style: TextStyle(fontSize: 14, color: Color(0xff4C4C4C)),
                    ),
                  ),
                ),
              ),
            ),
          ],
        );
      }
    } else {
      return Text(
        _text,
        maxLines: 3,
        textAlign: TextAlign.left,
        overflow: TextOverflow.ellipsis,
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return _RichText(widget.text!, widget.width);
  }
}
